package model

import (
	Mysql "gin/database"
	"gin/validate"
)

type DCCTools struct {
	Id       int64  `json:"id" gorm:"column:id"`
	TenantId int64  `json:"tenant_id" gorm:"column:tenant_id"`
	DCC      string `json:"dcc" gorm:"column:dcc"`
	Position string `json:"position" gorm:"column:position"`
	ToolsId  int64  `json:"tools_id" gorm:"column:tools_id"`
}

// 设置表名
func (DCCTools) TableName() string {
	return "dcc_tools"
}

// 插入dcc工具数据
func (this *DCCTools) Insert() (id int64, err error) {
	result := Mysql.DB.Create(&this)
	id = this.Id
	if result.Error != nil {
		err = result.Error
		return
	}
	return
}

// 通过id从DCC工具架移除工具
func (this *DCCTools) RemoveToolsFromDCCById(id int64) (err error) {
	result := Mysql.DB.Where("id = ?", id).Delete(&this)

	if result.Error != nil {
		err = result.Error
	}
	return
}

// 通过 tools_id 工具架移除工具
func (this *DCCTools) RemoveToolsFromDCCByToolsId(toolsId int64) (err error) {
	result := Mysql.DB.Where("tools_id = ?", toolsId).Delete(&this)

	if result.Error != nil {
		err = result.Error
	}
	return
}

// 查询指定DCC工具列表
func (this *DCCTools) GetDCCToolsList(param validate.GetDCCToolsListParam) (resData []DCCTools, err error) {
	dccTools := make([]DCCTools, 0)
	db := Mysql.DB

	db = db.Where("tenant_id = ?", param.TenantId)
	if param.DCC != "" {
		db = db.Where("dcc = ?", param.DCC)
	}

	if param.Position != "" {
		db = db.Where("position = ?", param.Position)
	}

	result := db.Select("id,tools_id").Find(&dccTools)
	if result.Error != nil {
		err = result.Error
		return
	}

	return dccTools, err
}
